package com.example.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.domain.Book;
import com.example.domain.BookRepository;
import com.example.properties.AmazonProperties;

@Controller
@RequestMapping("/")
public class ReadingController {

	
	private BookRepository bookRepository;
	private AmazonProperties amazonProperties;
	@Autowired
	public ReadingController(BookRepository bookRepository,AmazonProperties amazonProperties){
		this.bookRepository = bookRepository;
		this.amazonProperties = amazonProperties;
	}
	
	@RequestMapping(value = "/{reader}",method=RequestMethod.GET)
	public String readerBooks(@PathVariable("reader") String reader,Model model){
		List<Book> books = bookRepository.findByReader(reader);
		if(books!=null){
			model.addAttribute("books", books);
			model.addAttribute("amazonID", amazonProperties.getAssociateId());
		}
			return "readerList";
		
	}
	
	@RequestMapping(value="/{reader}",method=RequestMethod.POST)
	public String addToReadList(@PathVariable("reader") String reader,Book book){
		book.setReader(reader);
		bookRepository.save(book);
		return "redirect:/{reader}";
	}
}
